package com.coder520.common.utils;

import com.coder520.common.constants.Parameters;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author wsq
 *         Created by wsq on 2018-07-28 11:37.
 * @desc zookeeper分布式锁
 */
@Component
public class ZkClient {

    @Autowired
    private Parameters parameters;

    @Bean
    public CuratorFramework getZkClient(){
        CuratorFrameworkFactory.Builder builder=CuratorFrameworkFactory.builder()
                .connectString(parameters.getZkHost()) //拿到zookeeper连接
                .connectionTimeoutMs(3000)
                .retryPolicy(new RetryNTimes(5,500));  //重复5次 没有就挂
        CuratorFramework framework=builder.build();
        framework.start();
        return framework;
    }
}
